package com.example.xy_assistant.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.xy_assistant.R;
import com.example.xy_assistant.bean.Nav;
import java.util.List;

public class NavAdapter extends ArrayAdapter<Nav> {
    List<Nav> navs;
    Context context;

    public NavAdapter(@NonNull Context context, List<Nav> navs) {
        super(context, R.layout.item_main,navs);
        this.context = context;
        this.navs = navs;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Nav nav = navs.get(position);
        ViewHolder viewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_main, parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.iv_icon.setImageResource(nav.getImg());
        viewHolder.tv_text.setText(nav.getText());
        viewHolder.rootView.setBackgroundResource(nav.getColor());
        return convertView;
    }

    public static
    class ViewHolder {
        public View rootView;
        public ImageView iv_icon;
        public TextView tv_text;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_icon = (ImageView) rootView.findViewById(R.id.iv_icon);
            this.tv_text = (TextView) rootView.findViewById(R.id.tv_text);
        }

    }
}
